package ln.gather.jsr.jcache.integration;

import javax.cache.integration.CacheLoader;
import javax.cache.integration.CacheWriter;
import java.util.Comparator;

/**
 * 防止缓存击穿的一个补偿存储（兜底）
 *
 * @Author zhangj
 * @Date 2022/11/17 23:01
 */
public interface FallbackStorage<K,V> extends CacheLoader<K, V>, CacheWriter<K, V> {

    /**
     * 优先级比较器实例
     */
    Comparator<FallbackStorage> PRIORITY_COMPARATOR = new PriorityComparator();

    /**
     * 实现类的优先级
     * @return
     */
    int getPriority();

    void destroy();

    class PriorityComparator implements Comparator<FallbackStorage> {

        @Override
        public int compare(FallbackStorage o1, FallbackStorage o2) {
            return Integer.compare(o2.getPriority(), o1.getPriority());
        }
    }

}
